package com.ruoyi.security.service.impl;

import java.util.List;

import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.DateUtils;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.security.domain.vo.InspectionLocationVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.security.mapper.InspectionLocationMapper;
import com.ruoyi.security.domain.InspectionLocation;
import com.ruoyi.security.service.IInspectionLocationService;

/**
 * 巡检地点Service业务层处理
 *
 * @author cjie
 * @date 2022-04-14
 */
@Service
public class InspectionLocationServiceImpl implements IInspectionLocationService {
    @Autowired
    private InspectionLocationMapper inspectionLocationMapper;

    /**
     * 查询巡检地点
     *
     * @param id 巡检地点ID
     * @return 巡检地点
     */
    @Override
    public InspectionLocation selectInspectionLocationById(String id) {
        return inspectionLocationMapper.selectInspectionLocationById(id);
    }

    /**
     * 查询巡检地点列表
     *
     * @param inspectionLocation 巡检地点
     * @return 巡检地点
     */
    @Override
    public List<InspectionLocation> selectInspectionLocationList(InspectionLocation inspectionLocation) {
        return inspectionLocationMapper.selectInspectionLocationList(inspectionLocation);
    }

    /**
     * 新增巡检地点
     *
     * @param inspectionLocation 巡检地点
     * @return 结果
     */
    @Override
    public int insertInspectionLocation(InspectionLocation inspectionLocation) {
        if(StringUtils.isEmpty(inspectionLocation.getQrCode())){
            throw new CustomException("二维码不能为空");
        }
        InspectionLocationVO inspectionLocationVO = inspectionLocationMapper.selectInspectionLocationByQRCode(inspectionLocation.getType(),inspectionLocation.getQrCode());
        if(inspectionLocationVO != null){
            throw new CustomException("二维码已存在！");
        }
        inspectionLocation.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        inspectionLocation.setCreateTime(DateUtils.getNowDate());
        inspectionLocation.setCreateBy(SecurityUtils.getUsername());
        return inspectionLocationMapper.insertInspectionLocation(inspectionLocation);
    }

    /**
     * 修改巡检地点
     *
     * @param inspectionLocation 巡检地点
     * @return 结果
     */
    @Override
    public int updateInspectionLocation(InspectionLocation inspectionLocation) {
        inspectionLocation.setUpdateTime(DateUtils.getNowDate());
        inspectionLocation.setUpdateBy(SecurityUtils.getUsername());
        return inspectionLocationMapper.updateInspectionLocation(inspectionLocation);
    }

    /**
     * 批量删除巡检地点
     *
     * @param ids 需要删除的巡检地点ID
     * @return 结果
     */
    @Override
    public int deleteInspectionLocationByIds(String[] ids) {
        return inspectionLocationMapper.deleteInspectionLocationByIds(ids);
    }

    /**
     * 删除巡检地点信息
     *
     * @param id 巡检地点ID
     * @return 结果
     */
    @Override
    public int deleteInspectionLocationById(String id) {
        return inspectionLocationMapper.deleteInspectionLocationById(id);
    }

    @Override
    public InspectionLocationVO selectInspectionLocationByQRCoode(String type,String qrCode) {
        return inspectionLocationMapper.selectInspectionLocationByQRCode(type,qrCode);
    }
}
